import { superviseInfoAPI } from '@api/superviseInfo'
export default {
  data() {
    return {
      initFinish: false,
      iframeUrl: ''
    }
  },
  created: function() {
    this.init()
  },
  methods: {
    init() {
      superviseInfoAPI.getValueList({ vsCode: 'big_scree_url', appId: '' }).then(res => {
        let rescords = res.data
        if (rescords.length > 0) {
          const list = []
          rescords.forEach(item => {
            if (this.urlCode && this.urlCode.toUpperCase() == item.vsiKey.toUpperCase()) {
              list.push(item)
            }
          })
          if (list.length == 1) {
            superviseInfoAPI.getMiddlegroundUserInfo().then(res2 => {
              const params = {
                id: res2.userBasicVo.id,
                tenantCode: res2.userBasicVo.tenantCode
              }
              // 获取用户信息
              superviseInfoAPI.getCurrentSysInfo(params).then(res3 => {
                let currentUser = res3.data || {}
                let currentUserLdap = currentUser.userPurview.crsrmReporterLdap.toUpperCase()
                let ldap = {
                  ldap: currentUserLdap,
                  at: Math.round(new Date().getTime() / 1000)
                }
                // let buCode =
                //   currentUser.userPurview.crsrmPurchaserSbu.split(',').length > 1
                //     ? '50'
                //     : currentUser.userPurview.crsrmPurchaserSbu // 当前登录人所在BU的code
                let userCodeList = window.btoa(JSON.stringify(ldap)).split('')
                let firstCode = userCodeList[0]
                let fiveCode = userCodeList[4]
                userCodeList[0] = fiveCode
                userCodeList[4] = firstCode
                let base64Name = userCodeList.join('')
                // currentUserLdap进行Base64加密同时约定：第1位字符和第五位字符互换
                // href = href + '?buCode=' + buCode + '&ldap=' + base64Name
                // // 使用replace替换掉当前页面的href，防止浏览器有回退
                // window.location.replace(href)
                if(list[0].vsiKey.toUpperCase() == 'oeos-home'.toUpperCase()){
                  this.iframeUrl = list[0].vsiDescription + '?ldap=' + currentUserLdap +'&auth='+localStorage.Authorization;
                }else{
                  this.iframeUrl = list[0].vsiDescription + '?ldap=' + base64Name;
                }
                this.initFinish = true
              })
            })
          }
        }
      })
    }
  }
}
